<template>
  <div class="set_background">
    <div class="my_header">
      <div class="header_left">
        <img @click="goHome" src="@/assets/img/logo_1_1_162334.jpg" alt="" />
        <div>
          <span>合肥</span
          ><img src="@/assets/img/down_select_black191945.png" alt="" />
        </div>
      </div>
      <div class="header_middle">
        <input
          type="text"
          placeholder="搜您想要的"
          v-model="kw"
          @keyup.enter="goto"
        />
        <span @click="goto">搜索</span>
      </div>
      <div class="header_right" v-if="!rname">
        <router-link to="/login">登录</router-link>
        <span>|</span>
        <router-link to="/register">注册</router-link>
      </div>
      <div class="header_right" v-else>
        <div>
          欢迎:{{ rname }}
          <button @click="logout()">退出</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState } from "vuex";
export default {
  computed: {
    ...mapState(["rname"]),
  },
  data() {
    return {
      kw: "",
    };
  },

  methods: {
    logout() {
      this.updateRname("");
      sessionStorage.clear();
      this.$router.push("/");
    },
    ...mapMutations(["updateRname"]),
    goHome() {
      if (this.$route.fullPath === "/") return;
      this.$router.push("/");
    },
    goto() {
      const path = "/search/search_all?kw=" + this.kw;
      const pathEncode = "/search/search_all?kw=" + encodeURI(this.kw);
      // console.log(decodeURI(this.$route.fullPath.split("=")[1]))
      // console.log(encodeURI(this.kw))
      if (this.$route.fullPath === pathEncode) return;
      this.$router.push(path);
      // this.$router.go(0)
    },
  },
};
</script>

<style lang="scss" scoped>
.set_background {
  background-color: #fff;

  .my_header {
    width: 1200px;
    margin: 0 auto;
    height: 80px;
    display: flex;
    align-items: center;
    .header_left {
      height: 100%;
      display: flex;
      align-items: center;
      > img {
        height: 70%;
        margin-right: 25px;
      }
      > div {
        display: flex;
        align-items: center;
        > img {
          margin-left: 3px;
          height: 10px;
        }
      }
    }
    .header_middle {
      margin-left: 50px;
      display: flex;
      align-items: center;
      flex: 1;
      > input {
        height: 40px;
        width: 500px;
        outline: none;
        border: 1px solid #e4464b;
        padding: 0 15px;
        border-radius: 5px 0 0 5px;
      }
      > span {
        display: block;
        height: 42px;
        line-height: 42px;
        width: 80px;
        background-color: #e4464b;
        color: #fff;
        text-align: center;
        border-radius: 0 5px 5px 0;
        user-select: none;
        cursor: pointer;
      }
    }
    .header_right {
      > a {
        color: #000;
        margin: 0 5px;
        transition: 0.5s;
        &:hover {
          color: #e4464b;
        }
      }
    }
  }
}
button {
  border: none;
  background-color: transparent;
  font-size: 16px;
  color: gray;
  &:hover {
    color: #e4464b;
  }
}
</style>
